package view;

import interfaces.ViewPanel;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import utils.DataFormat;
import view.BackgroundPanel;
import control.CategoryManager;
import control.ExpenseManager;
import control.IncomeManager;
import control.LoanManager;
import control.MemberManager;
import control.PaymentMethodManager;
import control.Shell;


//@author A0103947A
public class ViewReportPanel extends BackgroundPanel implements ViewPanel  {
	
	private JLabel lblNumberIncomeEntries;
	
	private JLabel lblNumberMembers;
	private JLabel lblNumberCategories;
	private JLabel lblNumberPaymentMethods;
	private JLabel lblValueIncomeEntries;
	private JLabel lblNumberExpenseEntries;
	private JLabel lblCostOfExpenses;
	private JLabel lblIncomeSurplus;
	
	private JLabel lblCurrentActiveLoans;
	private JLabel lblClearedLoans;
	private JLabel lblNumberLoansBorrowed;
	private JLabel lblNetOverallLoss;
	private JLabel lblNumberLoansLent;
	private JLabel lblCurrentAmountLent;
	
	private JLabel lblCurrentMonthCostBorrow;
	
	private JLabel lblNumberInvestments;
	private JLabel lblCurrentValueInvestments;
	
	private ExpenseManager _expenseMan;
	private MemberManager _memberMan;
	private CategoryManager _catMan;
	private IncomeManager _incomeMan;
	private LoanManager _loanMan;
	private PaymentMethodManager _payMan;
	
	/**
	 * @param expenseMan
	 * @param importShell
	 * @param memberMan
	 * @param catMan
	 * @param incMan
	 * @param loanMan
	 * @param payMan
	 * @param savingsCalc
	 */
	public ViewReportPanel(ExpenseManager expenseMan,  Shell importShell, final MemberManager memberMan, 
			final CategoryManager catMan, IncomeManager incMan, LoanManager loanMan, 
			PaymentMethodManager payMan){
		_expenseMan = expenseMan;
		_memberMan = memberMan;
		_catMan = catMan;
		_incomeMan = incMan;
		_loanMan = loanMan;
		_payMan = payMan;
		setLayout(null);
		
		JLabel lblGeneralDetailsTitle = new JLabel("Transaction Details");
		lblGeneralDetailsTitle.setBounds(20, 11, 295, 25);
		lblGeneralDetailsTitle.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		this.add(lblGeneralDetailsTitle);
		
		JLabel lblNumberIncomeEntriesTitle = new JLabel("Number of Income Entries:");
		lblNumberIncomeEntriesTitle.setBounds(250, 41, 227, 16);
		lblNumberIncomeEntriesTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberIncomeEntriesTitle);
		
		lblNumberIncomeEntries = new JLabel(new Integer(_incomeMan.getList().size()).toString());
		lblNumberIncomeEntries.setBounds(482, 41, 112, 16);
		lblNumberIncomeEntries.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberIncomeEntries);
		
		JLabel lblValueIncomeEntriesTitle = new JLabel("Value of Income Entries:");
		lblValueIncomeEntriesTitle.setBounds(250, 62, 227, 16);
		lblValueIncomeEntriesTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblValueIncomeEntriesTitle);
		
		lblValueIncomeEntries = new JLabel("$" + DataFormat.returnTwoDecimals(new Double(_incomeMan.getTotalAmountOfAll())));
		lblValueIncomeEntries.setBounds(482, 62, 112, 16);
		lblValueIncomeEntries.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblValueIncomeEntries);
		
		JLabel lblNumberExpenseEntriesTitle = new JLabel("Number Of Expense Entries:");
		lblNumberExpenseEntriesTitle.setBounds(250, 83, 227, 16);
		lblNumberExpenseEntriesTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberExpenseEntriesTitle);
		
		lblNumberExpenseEntries = new JLabel(new Integer(_expenseMan.getList().size()).toString());
		lblNumberExpenseEntries.setBounds(482, 83, 112, 16);
		lblNumberExpenseEntries .setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberExpenseEntries);
		
		JLabel lblCostOfExpensesTitle = new JLabel("Cost of Expenses:");
		lblCostOfExpensesTitle.setBounds(364, 104, 113, 16);
		lblCostOfExpensesTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCostOfExpensesTitle);
		
		lblCostOfExpenses = new JLabel("$" + DataFormat.returnTwoDecimals(_expenseMan.getTotalAmountOfAll())+"");
		lblCostOfExpenses.setBounds(482, 104, 112, 16);
		lblCostOfExpenses.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblCostOfExpenses);
		
		JLabel lblNumberInvestmentsTitle = new JLabel("Number of Investments:");
		lblNumberInvestmentsTitle.setBounds(325, 125, 152, 16);
		lblNumberInvestmentsTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberInvestmentsTitle);
		
		lblNumberInvestments = new JLabel(_loanMan.numberOfInvestments()+"");
		lblNumberInvestments.setBounds(482, 125, 112, 16);
		lblNumberInvestments.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberInvestments);
		
		JLabel lblCurrentValueInvestmentsTitle = new JLabel("Current Value of Investments:");
		lblCurrentValueInvestmentsTitle.setBounds(290, 146, 187, 16);
		lblCurrentValueInvestmentsTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCurrentValueInvestmentsTitle);
		
		lblCurrentValueInvestments = new JLabel("$" + DataFormat.returnTwoDecimals(_loanMan.get_valueInvested()));
		lblCurrentValueInvestments.setBounds(482, 146, 112, 16);
		lblCurrentValueInvestments.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCurrentValueInvestments);
		
		JLabel lblNumberLoansLentTitle = new JLabel("Number of Loans Lent:");
		lblNumberLoansLentTitle.setBounds(334, 167, 143, 16);
		lblNumberLoansLentTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNumberLoansLentTitle);
		
		lblNumberLoansLent = new JLabel(_loanMan.get_totalLoansLent()+"");
		lblNumberLoansLent.setBounds(482, 167, 112, 16);
		lblNumberLoansLent.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberLoansLent);
		
		JLabel lblCurrentAmountLentTitle = new JLabel("Current Amount in Loans Lent:");
		lblCurrentAmountLentTitle.setBounds(206, 188, 271, 16);
		lblCurrentAmountLentTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCurrentAmountLentTitle);
		
		lblCurrentAmountLent = new JLabel("$" + DataFormat.returnTwoDecimals(_loanMan.get_amountFromLoansLent()));
		lblCurrentAmountLent.setBounds(482, 188, 112, 16);
		lblCurrentAmountLent.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCurrentAmountLent);
		
		JLabel lblIncomeSurplusTitle = new JLabel("Current Balance:");
		lblIncomeSurplusTitle.setBounds(250, 209, 227, 16);
		lblIncomeSurplusTitle.setFont(new Font("Century Gothic", Font.BOLD | Font.ITALIC, 13));
		lblIncomeSurplusTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblIncomeSurplusTitle);
		
		Double currentIncomeSurplus = new Double (new Double (_incomeMan.getTotalAmountOfAll()- _expenseMan.getTotalAmountOfAll() -
				_loanMan.get_valueInvested() - _loanMan.get_amountFromLoansLent()));
		String currentIncomeSurplusString;
		if (currentIncomeSurplus >= 0) {
			currentIncomeSurplusString = new String("$" + DataFormat.returnTwoDecimals(currentIncomeSurplus));
		}
		else {
			currentIncomeSurplusString = new String("-$" + DataFormat.returnTwoDecimals(Math.abs(currentIncomeSurplus)));
		}
		lblIncomeSurplus = new JLabel(currentIncomeSurplusString);
		lblIncomeSurplus.setBounds(482, 209, 112, 16);
		lblIncomeSurplus.setFont(new Font("Century Gothic", Font.BOLD | Font.ITALIC, 13));
		lblIncomeSurplus.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblIncomeSurplus);
		
		JLabel lblLoaninvsetmentDetails = new JLabel("Loan Details");
		lblLoaninvsetmentDetails.setBounds(20, 230, 245, 25);
		lblLoaninvsetmentDetails.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		add(lblLoaninvsetmentDetails);
		
		JLabel lblCurrentActiveLoansTitle = new JLabel("Current Active Loans:");
		lblCurrentActiveLoansTitle.setBounds(342, 260, 135, 16);
		lblCurrentActiveLoansTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblCurrentActiveLoansTitle);
		
		lblCurrentActiveLoans = new JLabel(_loanMan.get_totalLoans()+"");
		lblCurrentActiveLoans.setBounds(482, 260, 112, 16);
		lblCurrentActiveLoans.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblCurrentActiveLoans);
		
		JLabel lblClearedLoansTitle = new JLabel("Cleared Loans:");
		lblClearedLoansTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		lblClearedLoansTitle.setBounds(385, 281, 92, 16);
		add(lblClearedLoansTitle);
		
		lblClearedLoans = new JLabel(_loanMan.getTotalClearedLoans()+"");
		lblClearedLoans.setHorizontalAlignment(SwingConstants.TRAILING);
		lblClearedLoans.setBounds(482, 281, 112, 16);
		add(lblClearedLoans);
		
		JLabel lblNumberLoansBorrowedTitle = new JLabel("Number of Loans Borrowed:");
		lblNumberLoansBorrowedTitle.setBounds(303, 302, 174, 16);
		lblNumberLoansBorrowedTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNumberLoansBorrowedTitle);
		
		lblNumberLoansBorrowed = new JLabel(_loanMan.get_totalLoansBorrowed()+"");
		lblNumberLoansBorrowed.setBounds(482, 302, 112, 16);
		lblNumberLoansBorrowed.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberLoansBorrowed);
		
		JLabel lblNetOverallLossTitle = new JLabel("Net Overall Loss of Loans Borrowed:");
		lblNetOverallLossTitle.setBounds(250, 323, 227, 16);
		lblNetOverallLossTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNetOverallLossTitle);
		
		lblNetOverallLoss = new JLabel("$" + DataFormat.returnTwoDecimals(_loanMan.get_totalAmountBorrowed()));
		lblNetOverallLoss.setBounds(482, 323, 112, 16);
		lblNetOverallLoss.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNetOverallLoss);
		
		JLabel lblCurrentMonthCostBorrowTItle = new JLabel("Income Lost in Current Month from Loans Borrowed:");
		lblCurrentMonthCostBorrowTItle.setBounds(125, 344, 352, 16);
		lblCurrentMonthCostBorrowTItle.setFont(new Font("Century Gothic", Font.BOLD | Font.ITALIC, 13));
		lblCurrentMonthCostBorrowTItle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblCurrentMonthCostBorrowTItle);
		
		lblCurrentMonthCostBorrow = new JLabel("$" + DataFormat.returnTwoDecimals(_loanMan.get_monthlyPaymentForBorrowed()));
		lblCurrentMonthCostBorrow.setBounds(482, 344, 112, 16);
		lblCurrentMonthCostBorrow.setFont(new Font("Century Gothic", Font.BOLD | Font.ITALIC, 13));
		lblCurrentMonthCostBorrow.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCurrentMonthCostBorrow);
		
		JLabel lblSystemDetails = new JLabel("System Details");
		lblSystemDetails.setBounds(20, 380, 245, 25);
		lblSystemDetails.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		add(lblSystemDetails);
		
		JLabel lblNumberMembersTitle = new JLabel("Number of Members:");
		lblNumberMembersTitle.setBounds(345, 410, 132, 16);
		lblNumberMembersTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNumberMembersTitle);
		
		lblNumberMembers = new JLabel(new Integer(_memberMan.getList().size()).toString());
		lblNumberMembers.setBounds(482, 410, 112, 16);
		lblNumberMembers.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberMembers);
		
		JLabel lblNumberCategoriesTitle = new JLabel("Number of Categories:");
		lblNumberCategoriesTitle.setBounds(335, 431, 142, 16);
		lblNumberCategoriesTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNumberCategoriesTitle);
		
		lblNumberCategories = new JLabel(new Integer(_catMan.getList().size()).toString());
		lblNumberCategories.setBounds(482, 431, 112, 16);
		lblNumberCategories.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberCategories);
		
		JLabel lblNumberPaymentMethodsTitle = new JLabel("Number of Payment Methods:");
		lblNumberPaymentMethodsTitle.setBounds(291, 452, 186, 16);
		lblNumberPaymentMethodsTitle.setHorizontalAlignment(SwingConstants.TRAILING);
		add(lblNumberPaymentMethodsTitle);
		
		lblNumberPaymentMethods = new JLabel(new Integer(_payMan.getList().size()).toString());
		lblNumberPaymentMethods.setBounds(482, 452, 112, 16);
		lblNumberPaymentMethods.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNumberPaymentMethods);
	}
	
	/**
	 * 
	 * @param incomeManager
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updateIncome() {
		Integer totalNumberIncomeEntries = new Integer(_incomeMan.getList().size());
		lblNumberIncomeEntries.setText(totalNumberIncomeEntries.toString());
		
		Double totalValueIncomeEntries = new Double(_incomeMan.getTotalAmountOfAll());
		lblValueIncomeEntries.setText("$" + DataFormat.returnTwoDecimals(totalValueIncomeEntries));
		
		recalculateIncomeSurplus();
		
	}

	/**
	 * 
	 * @param expenseManager
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updateExpense() {
		
		Integer totalNumberExpenseEntries = new Integer(_expenseMan.getList().size());
		lblNumberExpenseEntries.setText(totalNumberExpenseEntries.toString());
		
		Double totalValueExpenseEntries = new Double(_expenseMan.getTotalAmountOfAll());
		lblCostOfExpenses.setText("$" + DataFormat.returnTwoDecimals(totalValueExpenseEntries));
		
		recalculateIncomeSurplus();
	}
	
	/**
	 * 
	 * @param savingsCalculator
	 * @Precondition 
	 * @Postcondition 
	 */
	
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void recalculateIncomeSurplus() {
		Double currentIncomeSurplus = new Double(_incomeMan.getTotalAmountOfAll() - _expenseMan.getTotalAmountOfAll() - _loanMan.get_valueInvested() - _loanMan.get_amountFromLoansLent());
		
		if (currentIncomeSurplus >= 0) {
			lblIncomeSurplus.setText("$" + DataFormat.returnTwoDecimals(currentIncomeSurplus));
		}
		else {
			lblIncomeSurplus.setText("-$" + DataFormat.returnTwoDecimals(Math.abs(currentIncomeSurplus)));
		}
		
	}

	/**
	 * 
	 * @param loanManager
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updateLoan() {
		lblCurrentActiveLoans.setText(_loanMan.get_totalLoans()+ "");
		lblClearedLoans.setText(_loanMan.getTotalClearedLoans()+"");
		
		lblNumberInvestments.setText(_loanMan.numberOfInvestments()+"");
		lblCurrentValueInvestments.setText("$" + DataFormat.returnTwoDecimals(_loanMan.get_valueInvested()));
		
		lblNumberLoansBorrowed.setText(_loanMan.get_totalLoansBorrowed()+"");
		lblNetOverallLoss.setText("$" + DataFormat.returnTwoDecimals(_loanMan.get_totalAmountBorrowed()));

		lblNumberLoansLent.setText(_loanMan.get_totalLoansLent()+"");
		lblCurrentAmountLent.setText("$" + DataFormat.returnTwoDecimals(_loanMan.get_amountFromLoansLent()));
		
		lblCurrentMonthCostBorrow.setText("$" + DataFormat.returnTwoDecimals(_loanMan.get_monthlyPaymentForBorrowed()));
		recalculateIncomeSurplus();
	}
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updateMember() {
		Integer totalMembers = new Integer(_memberMan.getList().size());
		lblNumberMembers.setText(totalMembers.toString());
	}
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updateCategory() {
		Integer totalCategories = new Integer(_catMan.getList().size());
		lblNumberCategories.setText(totalCategories.toString());
	}
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void updatePayMethod() {
		Integer totalPaymentMethods = new Integer(_payMan.getList().size());
		lblNumberPaymentMethods.setText(totalPaymentMethods.toString());
	}

	
	public void updateAll()
	{
		updateIncome();
		updateExpense();
		updateLoan();
		updateMember();
		updateCategory();
		updatePayMethod();
	}

	@Override
	public void updateTable() {
		// TODO Auto-generated method stub
		
	}
}
